unit uFoto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, DB, DBCtrls, Grids, DBGrids,
  XPMenu, dxCore, dxButton;//JpgImg;

type
  TfFoto = class(TForm)
    DBNavigator1: TDBNavigator;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Image1: TImage;
    XPMenu1: TXPMenu;
    Panel2: TPanel;
    Button3: TdxButton;
    Button2: TdxButton;
    Button1: TdxButton;
    dxButton4: TdxButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure dxButton4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
     codigofoto :string;
    { Public declarations }
  end;

var
  fFoto: TfFoto;

implementation

uses UDM, uPesquisa_Locacao, uPrincipal;

{$R *.dfm}

procedure TfFoto.Button1Click(Sender: TObject);
begin
  OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName);
  if OpenDialog1.Execute
   then Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;

procedure TfFoto.Button2Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.Assign(nil);
  if dm.cds_foto.Active then
  begin
   dm.cds_foto.Delete;
   dm.cds_foto.ApplyUpdates(0);
  end;

  if dm.cds_foto.Active then
    dm.cds_foto.Close;
  if dm.cds_imo_aluguel.Active then
  begin
    dm.cds_foto.Params[0].Clear;
    dm.cds_foto.Params[0].AsString :=
    IntTostr(dm.cds_imo_aluguelCOD_IMALU.AsInteger) + '-L';
  end
  else
    dm.cds_foto.Params[0].AsString :=
    IntTostr(fPesquisa_Locacao.scdsCp_procCOD_IMALU.AsInteger) + '-L';
    dm.cds_foto.Open;
  if not dm.cds_foto.IsEmpty then
  Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);
    
end;

procedure TfFoto.Button3Click(Sender: TObject);
begin
  if dm.cds_foto.Active then
    dm.cds_foto.Close;
    dm.cds_foto.Params[0].AsString := fFoto.codigofoto + '-L';
    dm.cds_foto.Open;
    if dm.cds_foto.IsEmpty then
    begin
     dm.cds_foto.Append;
     dm.cds_fotoCOD_IMOVEL.AsString := fFoto.codigofoto + '-L';
     dm.cds_fotoNOME_FOTO.AsString := OpenDialog1.FileName;
     dm.cds_foto.ApplyUpdates(0);
    end
    else
    begin
     if dm.cds_foto.Locate('NOME_FOTO',OpenDialog1.FileName,[loPartialKey])then
     begin
      dm.cds_foto.Edit;
      dm.cds_fotoCOD_IMOVEL.AsString := fFoto.codigofoto + '-L';
      dm.cds_fotoNOME_FOTO.AsString := OpenDialog1.FileName;
      dm.cds_foto.ApplyUpdates(0);
     end
     else
     begin
      dm.cds_foto.Append;
      dm.cds_fotoCOD_IMOVEL.AsString := fFoto.codigofoto + '-L';
      dm.cds_fotoNOME_FOTO.AsString := OpenDialog1.FileName;
      dm.cds_foto.ApplyUpdates(0);
     end;
    end;
end;

procedure TfFoto.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
begin
  Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);
end;

procedure TfFoto.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);
end;

procedure TfFoto.DBGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);
end;

procedure TfFoto.dxButton4Click(Sender: TObject);
begin
  close;
end;

procedure TfFoto.FormShow(Sender: TObject);
begin
  if dm.cds_foto.Active then
    dm.cds_foto.Close;
  dm.cds_foto.Params[0].Clear;
  dm.cds_foto.Params[0].AsString := fFoto.codigofoto + '-L';
  dm.cds_foto.Open;
  if not dm.cds_foto.IsEmpty then
    Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);
end;

procedure TfFoto.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   fFoto.codigofoto := '';
end;

end.
